package com.holly.unit.bpmn.modular.activiti.controller;

import com.holly.unit.auth.api.context.LoginContext;
import com.holly.unit.auth.api.pojo.login.LoginUser;
import com.holly.unit.bpmn.activiti.ActivitiOperator;
import com.holly.unit.bpmn.activiti.pojo.*;
import com.holly.unit.bpmn.activiti.util.ActUtil;
import com.holly.unit.bpmn.api.pojo.BpmnUser;
import com.holly.unit.bpmn.modular.activiti.adapter.LoginUserToBpmnUser;
import com.holly.unit.bpmn.modular.activiti.adapter.SysUserToComboModel;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import com.holly.unit.system.api.UserServiceApi;
import com.holly.unit.system.api.pojo.user.SysUserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 流程任务控制器
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/3/8 8:25
 */
@RestController
@ApiResource(name = "流程任务相关API")
@Api(tags = "流程任务相关API")
@Slf4j
public class ActTaskController {

    @Resource
    private UserServiceApi userServiceApi;

    @Resource
    private ActivitiOperator operator;

    @ApiOperation("代办列表")
    @GetResource(name = "代办列表", path = "/processTask/todoList")
    public ResponseData todoList(ProcessTaskToDoListRequest request) {
        LoginUser loginUser = LoginContext.me().getLoginUser();
        BpmnUser bpmnUser = LoginUserToBpmnUser.convert(loginUser);
        int firstResult = request.getPageNo();
        int maxResults = request.getPageSize();
        PageResult<ProcessTaskResponse> processTaskResponses = operator.todoList(request.getName(), request.getCategoryId(), request.getPriority(), bpmnUser, request.getCreateTimeBegin(), request.getCreateTimeEnd(),firstResult,maxResults);
        return new SuccessResponseData(processTaskResponses);
    }

    @ApiOperation("获取可返回的节点")
    @GetResource(name = "获取可返回的节点", path = "/processTask/getBackList")
    public ResponseData getBackList(@Validated(ProcessTaskGetBackListRequest.getBackList.class) ProcessTaskGetBackListRequest request) {
        List<HistoricTaskResponse> backList = operator.getBackList(request.getProcInstId());
        return new SuccessResponseData(backList);
    }

    @ApiOperation("任务节点审批 驳回至发起人")
    @PostResource(name = "任务节点审批 驳回至发起人", path = "/processTask/back")
    public ResponseData back(@RequestBody @Validated(ProcessTaskBackStarterRequest.backStarter.class) ProcessTaskBackStarterRequest request) {
        LoginUser loginUser = LoginContext.me().getLoginUser();
        BpmnUser bpmnUser = LoginUserToBpmnUser.convert(loginUser);
        operator.back(request.getId(),request.getProcInstId(),request.getComment(),request.getSendMessage(),request.getSendSms(),request.getSendEmail(),bpmnUser);
        return new SuccessResponseData();
    }

    @ApiOperation("流程流转历史")
    @GetResource(name = "流程流转历史", path = "/processTask/historicFlow")
    public ResponseData historicFlow(@Validated(ProcessTaskHistoricRequest.taskHisFlow.class) ProcessTaskHistoricRequest request) {
        List<HistoricTaskResponse> historicTaskResponses = operator.historicFlow(request.getId());
        return new SuccessResponseData(historicTaskResponses);
    }

    @ApiOperation("任务节点审批通过")
    @PostResource(name = "任务节点审批通过", path = "/processTask/pass")
    public ResponseData pass(@RequestBody @Validated(ProcessTaskPassRequest.pass.class) ProcessTaskPassRequest request) {
        LoginUser loginUser = LoginContext.me().getLoginUser();
        BpmnUser bpmnUser = LoginUserToBpmnUser.convert(loginUser);
        operator.pass(request.getId(),request.getProcInstId(),request.getAssignees(),request.getPriority(),request.getComment(),request.getSendMessage(),request.getSendSms(),request.getSendEmail(),bpmnUser);
        return new SuccessResponseData();
    }

    @ApiOperation("委托他人代办")
    @PostResource(name = "委托他人代办", path = "/processTask/delegate")
    public ResponseData delegate(@RequestBody @Validated(ProcessTaskDelegateRequest.delegate.class) ProcessTaskDelegateRequest request) {
        LoginUser loginUser = LoginContext.me().getLoginUser();
        BpmnUser bpmnUser = LoginUserToBpmnUser.convert(loginUser);
        operator.delegate(request.getId(),
                request.getUserId(),
                request.getProcInstId(),
                request.getComment(),
                request.getSendMessage(),
                request.getSendSms(),
                request.getSendEmail(),bpmnUser);
        return new SuccessResponseData();
    }

    @ApiOperation("任务节点审批驳回至指定历史节点")
    @PostResource(name = "任务节点审批驳回至指定历史节点", path = "/processTask/backToTask")
    public ResponseData backToTask(@RequestBody @Validated(ProcessTaskBackToTaskRequest.backToTask.class) ProcessTaskBackToTaskRequest request) {

        // 根据流程定义查询流程模型中是否包含网关
        if (ActUtil.isContainGateWay(request.getProcDefId())) {
            Map<String,Object> map = new HashMap<>();
            ActUtil.backToAnyActivity(request.getId(), request.getBackTaskKey(), request.getComment(),map);
        } else {
            LoginUser loginUser = LoginContext.me().getLoginUser();
            BpmnUser bpmnUser = LoginUserToBpmnUser.convert(loginUser);
            operator.backToTask(request.getId(),
                    request.getBackTaskKey(),
                    request.getProcInstId(),
                    request.getProcDefId(),
                    request.getAssignees(),
                    request.getPriority(),
                    request.getComment(),
                    request.getSendMessage(),
                    request.getSendSms(),
                    request.getSendEmail(),
                    bpmnUser);
        }
        return new SuccessResponseData();
    }

    @ApiOperation("已办列表")
    @GetResource(name = "已办列表", path = "/processTask/doneList")
    public ResponseData doneList(ProcessTaskDoneListQueryRequest request) {
        LoginUser loginUser = LoginContext.me().getLoginUser();
        List<SysUserDTO> users = userServiceApi.queryAllUserList(null);
        List<ComboModel> allUser = SysUserToComboModel.convert(users);
        int firstResult = request.getPageNo();
        int maxResults = request.getPageSize();
        PageResult<HistoricTaskResponse> historicTaskResponses = operator.doneList(request.getName(), request.getCategoryId(), request.getPriority(), request.getAppId(), loginUser, allUser,firstResult,maxResults);
        return new SuccessResponseData(historicTaskResponses);
    }

    @ApiOperation("任务节点审批驳回至指定历史节点")
    @PostResource(name = "任务节点审批驳回至指定历史节点", path = "/processTask/deleteHistoric")
    public ResponseData deleteHistoric(@RequestBody @Validated(ProcessTaskDeleteHistoricRequest.deleteHistoric.class) ProcessTaskDeleteHistoricRequest request) {
        operator.deleteHistoric(request.getIds());
        return new SuccessResponseData();
    }

}
